#include <cstdio>

int a[ 1000001 ], b[ 10001 ], next[ 10001 ];

void getnext( int *a, int *next, int n )
{
    int i, j = -1;
    next[ 0 ] = -1;
    for ( i = 1; i < n; i++ )
    {
        while ( j >= 0 && a[ j + 1 ] != a[ i ] ) j = next[ j ];
        if ( a[ j + 1 ] == a[ i ] ) j++;
        next[ i ] = j;
    }
}

int match( int *a, int *b, int n, int m, int *next )
{
    int i, j = -1;
    for ( i = 0; i < n; i++ )
    {
        while ( j >= 0 && b[ j + 1 ] != a[ i ] ) j = next[ j ];
        if ( b[ j + 1 ] == a[ i ] ) j++;
        if ( j == m - 1 ) return i + 2 - m;
    }
    return -1;
}

int main( )
{
    int t, n, m, i;
    scanf("%d", &t);
    while ( t-- )
    {
        scanf("%d%d", &n, &m);
        for ( i = 0; i < n; i++ )
            scanf("%d", &a[ i ]);
        for ( i = 0; i < m; i++ )
            scanf("%d", &b[ i ]);
        getnext( b, next, m );
        printf("%d\n", match( a, b, n, m, next ));
    }
    return 0;
}
